package cc.bukkitPlugin.banitem.api;

import java.util.HashMap;
import java.util.HashSet;

import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;

import cc.bukkitPlugin.banitem.BanItem;

public class ModAPIMan{

    private static HashMap<Class<?>,AMod> mMods=new HashMap<>();
    private static boolean init=false;

    static{
        ModAPIMan.init();
    }

    public static void init(){
        if(ModAPIMan.init)
            return;
        ModAPIMan.register(new ModBaubles());
        ModAPIMan.register(new ModTConstruct());
        ModAPIMan.init=true;
    }

    public static void register(AMod pModel){
        if(pModel.initSuccess()){
            ModAPIMan.mMods.put(pModel.getClass(),pModel);
            BanItem.debug("注册了背包模块: "+pModel.getClass().getSimpleName());
        }
    }

    /**
     * 获取一个玩家所有MOD的背包
     * @param pPlayer 玩家
     * @return 背包,肯定不为null,也不包含null
     */
    public static HashSet<Inventory> getModInventory(Player pPlayer){
        HashSet<Inventory> tAllModInvs=new HashSet<>();
        for(AMod sAModel : ModAPIMan.mMods.values()){
            try{
                HashSet<Inventory> tModInvs=sAModel.getInventorys(pPlayer);
                BanItem.debug("背包模块["+sAModel.getClass().getSimpleName()+"]返回了"+(tModInvs==null?0:tModInvs.size())+"个背包");
                if(tModInvs==null||tModInvs.isEmpty())
                    continue;
                tAllModInvs.addAll(tModInvs);
            }catch(Throwable exp){
                BanItem.severe("获取"+sAModel.getClass().getSimpleName()+"背包模块的背包时发生了错误",exp);
            }
        }
        tAllModInvs.remove(null);
        return tAllModInvs;
    }

}
